//
//  LoginViewController.m
//  WangLottery
//
//  Created by 黄修勇 on 12-5-4.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginSuccessViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "LineLabelControl.h"
#import "UserRegViewController.h"
#import "ForgotPswViewController.h"

//#import "WWRequestAPI.h"
#import "WWRequestAPI+WWAccountAPI.h"

@interface LoginViewController ()

-(void)loginSuccess;

@end

@implementation LoginViewController


@synthesize loginTableView=_loginTableView;
@synthesize forgotPsw=_forgotPsw;
@synthesize regisAcc=_regisAcc;

-(void)dealloc{
    [[WWRequestAPI currentRequestAPI] cancelRequestWithTag:requestTag];
    [_loginTableView release];
    [_forgotPsw release];
    [_regisAcc release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title=@"账户登陆";
    [self.view setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.96 alpha:1]];
    
    _loginTableView.rowHeight=35.0f;
    _loginTableView.separatorColor=[UIColor clearColor];
    _regisAcc.hidden = YES;
    
    _regisAcc.text=@"注册帐号";
    
    _forgotPsw.text=@"忘记密码?";
    
    // Do any additional setup after loading the view from its nib.
}

//登陆成功

-(void)loginSuccess{
    
    LoginSuccessViewController *loginIn=[[LoginSuccessViewController alloc]initWithNibName:@"LoginSuccessViewController" bundle:nil];
    NSArray *array=[NSArray arrayWithObject:loginIn];
    /*
     CATransition *animation =[CATransition animation];
     [animation setDuration:0.5f];
     [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
     [animation setType:kCATransitionFade];
     [animation setSubtype:kCATransitionFromBottom];
     */
    [self.navigationController setViewControllers:array animated:YES];
    
    //  self.navigationController.view.layer
}

-(IBAction)registerAccount:(id)sender
{
    UserRegViewController *user=[[UserRegViewController alloc]initWithNibName:@"UserRegViewController" bundle:nil];
    [self.navigationController pushViewController:user animated:YES];
    [user release];
}
-(IBAction)forgotPsw:(id)sender{
    
    ForgotPswViewController *forgotPsw=[[ForgotPswViewController alloc]initWithNibName:@"ForgotPswViewController" bundle:nil];
    [self.navigationController pushViewController:forgotPsw animated:YES];
    [forgotPsw release];
}
//登录按钮
-(IBAction)Login:(id)sender
{
    //账号和密码的控件
    UITextField *usr = (UITextField*)[[self.loginTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:10];
    UITextField *psw = (UITextField*)[[self.loginTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] viewWithTag:10];
    
    NSLog(@"login with user:%@  withPsw:%@",usr.text,psw.text);
    
    if(!usr.text || !psw.text || [usr.text length] != 11)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入完整" message:@"请输入完整的用户名和密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    
    requestTag = [[WWRequestAPI currentRequestAPI] loginWithusername:usr.text psw:psw.text target:self];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -UITableViewDataSource


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 35.0f;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //行数少了 不用重用
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"account"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.backgroundColor=[UIColor clearColor];
    
    //colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.4f];
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(20.0f, 10.0f, 0.0f, 0.0f)];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.font=[UIFont systemFontOfSize:14.0f];
    
    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(80.0f, 10.0f, 200.0f, 31.0f)];
    textField.font=[UIFont systemFontOfSize:14.0f];
    textField.clearButtonMode=UITextFieldViewModeWhileEditing;
    textField.returnKeyType=UIReturnKeyDone;
    textField.delegate=self;
    textField.tag=10;
    //如果是密码的，要加密显示
    if(indexPath.row == 1)
        textField.secureTextEntry = YES;
    
    if (indexPath.row==0) {
        titleLabel.text=@"用户名";
        textField.placeholder=@"手机号";
        textField.keyboardType=UIKeyboardTypeNumberPad;
    }else {
        titleLabel.text=@"密码";
        textField.placeholder=@"请输入您的密码";
    }
    
    [titleLabel sizeToFit];
    [cell.contentView addSubview:titleLabel];
    [titleLabel release];
    [cell addSubview:textField];
    [textField release];
    
    return cell;
}

#pragma mark-UITextFiledDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark -

-(void)requesResult:(NSDictionary *)dic{
    
    NSLog(@"dic %@",dic);
    if([[dic objectForKey:@"result"] isEqualToString:@"0"])
        [self loginSuccess];
    else
        NSLog(@"error:%@",[dic objectForKey:@"result"]);
}
@end
